/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.citylifeprot.domain;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
import com.badlogic.gdx.math.Vector2;
import com.citylifeprot.Assets.Assets;

/**
 *
 * @author Cypher
 */
public class AntEntity extends Sprite //implements InputProcessor {
{
    /**
     * Ant Parameters
     */
    
    private float alpha;
    private float beta;
    private float gamma;
    private boolean haveFood;
    private int foodCollected;

    /**
     * the movement velocity
     */
    private Vector2 velocity = new Vector2();

    private float speed = 60 * 2, gravity = 60 * 1.8f, animationTime = 0, increment;

    private boolean canJump;

    private Animation still, left, right;
    private TiledMapTileLayer collisionLayer;
    private Texture staticAnt;

    private String blockedKey = "Blocked";

    public AntEntity(TiledMapTileLayer collisionLayer)
    {
        this.collisionLayer = collisionLayer;
        this.staticAnt = Assets.antCart;
    }

    public AntEntity(Animation still, Animation left, Animation right, TiledMapTileLayer collisionLayer)
    {
        super(still.getKeyFrame(0));
        this.still = still;
        this.left = left;
        this.right = right;
        this.collisionLayer = collisionLayer;
        setSize(collisionLayer.getWidth() / 3, collisionLayer.getHeight() * 1.25f);
    }

    public void draw(SpriteBatch spriteBatch)
    {
        update(Gdx.graphics.getDeltaTime());
        super.draw(spriteBatch);
    }

    public void update(float delta)
    {

        // clamp velocity
        if (velocity.y > speed)
            velocity.y = speed;
        else if (velocity.y < -speed)
            velocity.y = -speed;

        // save old position
        float oldX = getX(), oldY = getY();
        boolean collisionX = false, collisionY = false;

        // move on x
        setX(getX() + velocity.x * delta);

        // calculate the increment for step in #collidesLeft() and #collidesRight()
        increment = collisionLayer.getTileWidth();
        increment = getWidth() < increment ? getWidth() / 2 : increment / 2;

        if (velocity.x < 0) // going left
            collisionX = collidesLeft();
        else if (velocity.x > 0) // going right
            collisionX = collidesRight();

        // react to x collision
        if (collisionX)
        {
            setX(oldX);
            velocity.x = 0;
        }

        // move on y
        setY(getY() + velocity.y * delta * 5f);

        // calculate the increment for step in #collidesBottom() and #collidesTop()
        increment = collisionLayer.getTileHeight();
        increment = getHeight() < increment ? getHeight() / 2 : increment / 2;

        if (velocity.y < 0) // going down
            canJump = collisionY = collidesBottom();
        else if (velocity.y > 0) // going up
            collisionY = collidesTop();

        // react to y collision
        if (collisionY)
        {
            setY(oldY);
            velocity.y = 0;
        }

        // update animation
        animationTime += delta;
        //setRegion(velocity.x < 0 ? left.getKeyFrame(animationTime) : velocity.x > 0 ? right.getKeyFrame(animationTime) : still.getKeyFrame(animationTime));
        this.setRegion(staticAnt);
    }

    private boolean isCellBlocked(float x, float y)
    {
        Cell cell = collisionLayer.getCell((int) (x / collisionLayer.getTileWidth()), (int) (y / collisionLayer.getTileHeight()));
        return cell != null;// && cell.getTile() != null && cell.getTile().getProperties().containsKey(blockedKey);
    }

    public boolean collidesRight()
    {
        for (float step = 0; step <= getHeight(); step += increment)
        {
            if (isCellBlocked(getX() + getWidth(), getY() + step))
                return true;
        }
        return false;
    }

    public boolean collidesLeft()
    {
        for (float step = 0; step <= getHeight(); step += increment)
        {
            if (isCellBlocked(getX(), getY() + step))
                return true;
        }
        return false;
    }

    public boolean collidesTop()
    {
        for (float step = 0; step <= getWidth(); step += increment)
        {
            if (isCellBlocked(getX() + step, getY() + getHeight()))
                return true;
        }
        return false;

    }

    public boolean collidesBottom()
    {
        for (float step = 0; step <= getWidth(); step += increment)
        {
            if (isCellBlocked(getX() + step, getY()))
                return true;
        }
        return false;
    }

    public Vector2 getVelocity()
    {
        return velocity;
    }

    public void setVelocity(Vector2 velocity)
    {
        this.velocity = velocity;
    }

    public float getSpeed()
    {
        return speed;
    }

    public void setSpeed(float speed)
    {
        this.speed = speed;
    }

    public float getGravity()
    {
        return gravity;
    }

    public void setGravity(float gravity)
    {
        this.gravity = gravity;
    }

    public TiledMapTileLayer getCollisionLayer()
    {
        return collisionLayer;
    }

    public void setCollisionLayer(TiledMapTileLayer collisionLayer)
    {
        this.collisionLayer = collisionLayer;
    }

}
